/**
 * Created by seth.yang on 2017/2/23
 */
$(function () {
    Array.prototype.insert = function (index, item) {
        this.splice(index, 0, item);
    };
    const MAX_COUNT = 30;
    var table = $('#data-table'), service = URL_BASE + '/ws/devices', socket = new WebSocketWrapper ({
        onopen : function () {console.log ('web socket opened.')},
        onmessage : function (message) {
            if (message == '[]') return;
            var data = eval ('(' + message + ')');
            console.log (data);
            data = data.length ? data : [data];
            for (var i = data.length - 1; i >= 0; i --) {
                logs.insert (0, data [i]);
            }
            if (logs.length > MAX_COUNT) {
                logs.length = MAX_COUNT;
            }

            build ();
        },
        onerror : function (e) {
            console.log (e);
        },
        onclose : function () {console.log ('web socket closed.')}
    }).connect (service), logs = [], imei = $('#imei'), imsi = $('#imsi');

    function build () {
        table.empty ();
        buildHeader ();

        for (var i = 0; i < logs.length; i ++) {
            buildLine (logs [i]);
        }
    }

    function buildHeader () {
        var tr = $('<tr />').appendTo (table);
        $('<td class="data-cell"/>').text ('IMEI').appendTo (tr);
        $('<td class="data-cell"/>').text ('IMSI').appendTo (tr);
        $('<td class="data-cell"/>').text ('Type').appendTo (tr);
        $('<td class="data-cell"/>').text ('Serial No').appendTo (tr);
        $('<td class="data-cell"/>').text ('Timestamp').appendTo (tr);
        $('<td class="data-cell"/>').text ('Battery').appendTo (tr);
        $('<td class="data-cell"/>').text ('Signal').appendTo (tr);
        $('<td class="data-cell"/>').text ('ECL').appendTo (tr);
        $('<td class="data-cell"/>').text ('VOC').appendTo (tr);
        $('<td class="data-cell"/>').text ('温度(℃)').appendTo (tr);
        $('<td class="data-cell"/>').text ('湿度(%)').appendTo (tr);
        $('<td class="data-cell"/>').text ('CO2(ppm)').appendTo (tr);
        $('<td class="data-cell"/>').text ('PM2.5(ug/m3)').appendTo (tr);
    }

    function buildLine (data) {
        var tr = $('<tr />').appendTo (table), a = data ['action'], i, b = parseInt (data['battery'], 10),
            f = (a == 'FireAlarm'), action = ((f && b) ? 'Reply' : a),
            snr = data ['snr'];
        if (!snr && snr != 0) snr = 'N/A';
        else {
            snr = parseInt (snr, 10);
            if (isNaN (snr)) snr = 'N/A';
            else {
                if (snr == 0xff) snr = 'N/A';
                else {
                    var s = (snr & 0x80) != 0;
                    snr = snr & 0x7f;
                    if (s) snr = -snr;
                }
            }
        }
        $('<td class="data-cell"/>').text (data['imei']).appendTo (tr);
        $('<td class="data-cell"/>').text (data['imsi']).appendTo (tr);
        $('<td class="data-cell"/>').text (action).appendTo (tr);
        $('<td class="data-cell"/>').text (data['serialNo']).appendTo (tr);
        $('<td class="data-cell"/>').text (data['uploadTime']).appendTo (tr);
        $('<td class="data-cell"/>').text (f ? '' : b + '%').appendTo (tr);
        $('<td class="data-cell"/>').text (f ? '' : '-' + data['signal'] + 'dB').appendTo (tr);
        $('<td class="data-cell"/>').text (f ? '' : data['coverage']).appendTo (tr);
        $('<td class="data-cell"/>').text (f ? '' : snr).appendTo (tr);
        $('<td class="data-cell"/>').text (f ? '' : data['pci']).appendTo (tr);
        $('<td class="data-cell"/>').text (f ? '' : data['rsrq']).appendTo (tr);
        $('<td class="data-cell"/>').text (f ? '' : data['cellId']).appendTo (tr);
        $('<td class="data-cell"/>').text (f ? '' : data['earFcn']).appendTo (tr);
        if (f) {
            for (i = 0; i < tr [0].cells.length; i ++) {
                $(tr[0].cells [i]).addClass (b == 0 ? 'Alarm' : 'warn');
            }
        }
    }

    $('#btn-apply').on ('click', function () {
        logs = [];
        build ();
        var filter = {};
        if (imei.val ()) {
            filter ['imei'] = imei.val ();
        }
        if (imsi.val ()) {
            filter ['imsi'] = imsi.val ();
        }
        var text = JSON.stringify (filter);
        console.log (text);
        socket.send (text);
    });

    $('#imei,#imsi').on ('keyup', function (e) {
        if (e.keyCode == 13) {
            $('#btn-apply').trigger ('click');
        }
    });
});